home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / lib / Gen / genbki.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  1992-08-27  |  4.0 KB  |  205 lines

  1. #! /bin/sh
  2. # ----------------------------------------------------------------
  3. #   FILE
  4. #    genbki.sh
  5. #
  6. #   DESCRIPTION
  7. #       shell script which generates .bki files from specially
  8. #    formatted .h files.  These .bki files are used to initialize
  9. #    the postgres template database.
  10. #
  11. #   NOTES
  12. #    non-essential whitespace is removed from the generated file.
  13. #    if this is ever a problem, then the sed script at the very
  14. #    end can be changed into another awk script or something smarter..
  15. #
  16. #   IDENTIFICATION
  17. #     $Header: /private/postgres/src/lib/Gen/RCS/genbki.sh,v 1.4 1992/06/23 18:24:51 mer Exp $
  18. # ----------------------------------------------------------------
  19.  
  20. # -----------------
  21. # check for options i.e. -D defines for cpp
  22. # -----------------
  23. BKIOPTS=''
  24. set - `getopt D: $*`
  25. if [ $? != 0 ]
  26. then
  27.     echo `basename $0`: Bad option
  28.     exit 1
  29. fi
  30.  
  31. for opt in $*
  32. do
  33.     case $opt in
  34.     -D) BKIOPTS="$BKIOPTS -D$2"; shift; shift;;
  35.     --) shift; break;;
  36.     esac
  37. done
  38.  
  39. # ----------------
  40. #     collect nodefiles
  41. # ----------------
  42. SYSFILES=''
  43. x=1
  44. numargs=$#
  45. while test $x -le $numargs ; do
  46.     SYSFILES="$SYSFILES $1"
  47.     x=`expr $x + 1`
  48.     shift
  49. done
  50.  
  51. # ----------------
  52. #     strip comments and trash from .h before we generate
  53. #    the .bki file...
  54. # ----------------
  55. #
  56. cat $SYSFILES | \
  57. sed -e 's/\/\*.*\*\///g' \
  58.     -e 's/;[     ]*$//g' | \
  59. awk '
  60. # ----------------
  61. #    now use awk to process remaining .h file..
  62. #
  63. #    nc is the number of catalogs
  64. #    inside is a variable set to 1 when we are scanning the
  65. #       contents of a catalog definition.
  66. #    inserting_data is a flag indicating when we are processing DATA lines.
  67. #        (i.e. have a relation open and need to close it)
  68. # ----------------
  69. BEGIN {
  70.     inside = 0;
  71.     raw = 0;
  72.     bootstrap = 0;
  73.     nc = 0;
  74.     reln_open = 0;
  75. }
  76.  
  77. # ----------------
  78. #    anything in a BKI_BEGIN .. BKI_END block should be passed
  79. #    along without interpretation.
  80. # ----------------
  81. /^BKI_BEGIN/     { raw = 1; next; }
  82. /^BKI_END/     { raw = 0; next; }
  83. raw == 1     { print; next; }
  84.  
  85. # ----------------
  86. #    DATA() statements should get passed right through after
  87. #    stripping off the DATA( and the ) on the end.
  88. # ----------------
  89. /^DATA\(/ {
  90.     data = substr($0, 6, length($0) - 6);
  91.     print data;
  92.     next;
  93. }
  94.  
  95. /^DEFINE_INDEX\(/ {
  96. # ----
  97. #  end any prior catalog data insertions before starting a define index
  98. # ----
  99.     if (reln_open == 1) {
  100.         print "show";
  101.         print "close " catalog;
  102.         reln_open = 0;
  103.     }
  104.  
  105.     data = substr($0, 14, length($0) - 14);
  106.     print "define index " data
  107. }
  108.     
  109. # ----------------
  110. #    CATALOG() definitions take some more work.
  111. # ----------------
  112. /^CATALOG\(/ { 
  113. # ----
  114. #  end any prior catalog data insertions before starting a new one..
  115. # ----
  116.     if (reln_open == 1) {
  117.         print "show";
  118.         print "close " catalog;
  119.         reln_open = 0;
  120.     }
  121.  
  122. # ----
  123. #  get the name of the new catalog
  124. # ----
  125.     pos = index($1,")");
  126.     catalog = substr($1,9,pos-9); 
  127.  
  128.     if ($0 ~ /BOOTSTRAP/) {
  129.         bootstrap = 1;
  130.     }
  131.  
  132.         i = 1;
  133.     inside = 1;
  134.         nc++;
  135.     next;
  136. }
  137.  
  138. # ----------------
  139. #    process the contents of the catalog definition
  140. #
  141. #    attname[ x ] contains the attribute name for attribute x
  142. #    atttype[ x ] contains the attribute type fot attribute x
  143. # ----------------
  144. inside == 1 {
  145. # ----
  146. #  ignore a leading brace line..
  147. # ----
  148.         if ($1 ~ /{/)
  149.         next;
  150.  
  151. # ----
  152. #  if this is the last line, then output the bki catalog stuff.
  153. # ----
  154.     if ($1 ~ /}/) {
  155.         if (bootstrap) {
  156.             print "create bootstrap " catalog;
  157.         } else {
  158.             print "create " catalog;
  159.         }
  160.         print "\t(";
  161.  
  162.         for (j=1; j<i-1; j++) {
  163.             print "\t " attname[ j ] " = " atttype[ j ] " ,";
  164.         }
  165.         print "\t " attname[ j ] " = " atttype[ j ] ;
  166.         print "\t)";
  167.  
  168.         if (! bootstrap) {
  169.             print "open " catalog;
  170.         }
  171.  
  172.         i = 1;
  173.         reln_open = 1;
  174.         inside = 0;
  175.         bootstrap = 0;
  176.         next;
  177.     }
  178.  
  179. # ----
  180. #  if we are inside the catalog definition, then keep sucking up
  181. #  attibute names and types
  182. # ----
  183.     atttype[ i ] = $1;
  184.     attname[ i ] = $2;
  185.     i++;
  186.     next;
  187. }
  188.  
  189. END {
  190.     if (reln_open == 1) {
  191.         print "show";
  192.         print "close " catalog;
  193.         reln_open = 0;
  194.     }
  195. }
  196. ' | \
  197. /lib/cpp $BKIOPTS | \
  198. sed -e '/^[     ]*$/d' \
  199.     -e 's/[     ][     ]*/ /g'
  200.  
  201. # ----------------
  202. #    all done
  203. # ----------------
  204. exit 0
  205.